package com.airconditioner.sales.weixin;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.util.Map;

public class GetPrePayIdTasks extends
		AsyncTask<Void, Void, Map<String, String>> {
	private ProgressDialog dialog;
	private Context mContext;
	WeixinPayUtils payUtils;
	Map<String, String> resultunifiedorder;
	private String mBody, mTotal_free;

	public GetPrePayIdTasks(Context context, String body, String total_free) {
		mContext = context;
		mBody = body;
		mTotal_free = total_free;
	}

	@Override
	protected void onPreExecute() {
		dialog = new ProgressDialog(mContext);
		dialog.setTitle("提示");
		dialog.setMessage("正在获取预支付订单...");
		dialog.show();
		payUtils = new WeixinPayUtils(mContext, mBody, mTotal_free);
	}

	@Override
	protected void onPostExecute(Map<String, String> result) {
		if (dialog != null) {
			dialog.dismiss();
		}
		// sb.append("prepay_id\n" + result.get("prepay_id") + "\n\n");
		// show.setText(sb.toString());
		resultunifiedorder = result;
		payUtils.genPayReq(result);
		payUtils.sendPayReq();
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
	}

	@Override
	protected Map<String, String> doInBackground(Void... params) {

		String url = String
				.format("https://api.mch.weixin.qq.com/pay/unifiedorder");
		String entity = payUtils.genProductArgs();

		Log.e("orion", entity);

		byte[] buf = Util.httpPost(url, entity);

		String content = new String(buf);
		Log.e("orion", content);
		Map<String, String> xml = payUtils.decodeXml(content);
		return xml;
	}
}
